﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;

namespace WUSTL.SharePoint.WebParts.AnonInfoPath.FormViewWebPart
{
    class AnonInfoPathEditor : EditorPart
    {
        public static string afvwpSubmitUserKey;
        public static string userAccount
        {
            get
            {
                SPWeb web;
                SPWeb rootWeb = null;

                //Work with the web
                web = SPContext.Current.Web;

                if (web.IsRootWeb)
                {
                    //Check to see if the key exists
                    afvwpSubmitUserKey = web.ID.ToString() + "_SubmitUser";
                    if (web.AllProperties.ContainsKey(afvwpSubmitUserKey))
                    {
                        //Ensure value is not null or empty
                        if (!String.IsNullOrEmpty(web.AllProperties[afvwpSubmitUserKey].ToString()))
                        {
                            string _user = web.AllProperties[afvwpSubmitUserKey].ToString();
                            return _user;
                        }
                        else
                        {
                            return string.Empty;
                        }
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                else
                {
                    do
                    {
                        if (web.IsRootWeb)
                        {
                            rootWeb = web;
                        }
                        else
                        {
                            web = web.ParentWeb;
                            rootWeb = null;
                        }
                    }
                    while (rootWeb == null);
                    afvwpSubmitUserKey = rootWeb.ID.ToString() + "_SubmitUser";
                    if (rootWeb.AllProperties.ContainsKey(afvwpSubmitUserKey))
                    {
                        //Ensure value is not null or empty
                        if (!String.IsNullOrEmpty(web.AllProperties[afvwpSubmitUserKey].ToString()))
                        {
                            string _user = rootWeb.AllProperties[afvwpSubmitUserKey].ToString();
                            rootWeb.Dispose();
                            return _user;
                        }
                        else
                        {
                            rootWeb.Dispose();
                            return string.Empty;
                        }
                    }
                    else
                    {
                        rootWeb.Dispose();
                        return string.Empty;
                    }
                }
            }
        }

        //Input controls
        AssetUrlSelector xsnUriAus;
        AssetUrlSelector submitLocationAus;
        AssetUrlSelector cssLocationAus;
        DropDownList successActionDdl;
        TextBox customHtmlTxt;
        AssetUrlSelector redirectUrlAus;
        CheckBox sendAsEmailChk;
        CheckBox enableQueryStringChk;
        TextBox queryStringParameterTxt;
        TextBox queryStringFormFieldTxt;

        //Labels
        Label xsnUriLbl;
        Label submitLocationLbl;
        Label cssLocationLbl;
        Label successActionLbl;
        Label customHtmlLbl;
        Label redirectUrlLbl;
        Label sendAsEmailLbl;
        Label enableQueryStringLbl;
        Label queryStringParameterLbl;
        Label queryStringFormFieldLbl;

        //Panels
        Panel customHtmlPnl;
        Panel redirectUrlPnl;
        Panel noActionPnl;
        Panel queryStringPnl;

        //Buttons
        Button textBuilderBtn;

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            FormViewWebPart wp = WebPartToEdit as FormViewWebPart;

            wp.xsnURI = xsnUriAus.AssetUrl;
            wp.submitLocation = submitLocationAus.AssetUrl;
            wp.cssLocation = cssLocationAus.AssetUrl;
            wp.successDdl = successActionDdl.SelectedValue;
            wp.customHtml = customHtmlTxt.Text;
            wp.redirectUrl = redirectUrlAus.AssetUrl;
            wp.sendAsEmail = sendAsEmailChk.Checked;
            wp.enableQueryString = enableQueryStringChk.Checked;
            wp.queryStringParameter = queryStringParameterTxt.Text;
            wp.queryStringFormField = queryStringFormFieldTxt.Text;

            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            FormViewWebPart wp = WebPartToEdit as FormViewWebPart;

            xsnUriAus.AssetUrl = wp.xsnURI;
            submitLocationAus.AssetUrl = wp.submitLocation;
            cssLocationAus.AssetUrl = wp.cssLocation;
            successActionDdl.SelectedValue = wp.successDdl;
            customHtmlTxt.Text = wp.customHtml;
            redirectUrlAus.AssetUrl = wp.redirectUrl;
            sendAsEmailChk.Checked = wp.sendAsEmail;
            enableQueryStringChk.Checked = wp.enableQueryString;
            queryStringParameterTxt.Text = wp.queryStringParameter;
            queryStringFormFieldTxt.Text = wp.queryStringFormField;
        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();

            //Create the controls
            //Inputs
            xsnUriAus = new AssetUrlSelector();
            submitLocationAus = new AssetUrlSelector();
            cssLocationAus = new AssetUrlSelector();
            successActionDdl = new DropDownList();
            customHtmlTxt = new TextBox();
            redirectUrlAus = new AssetUrlSelector();
            sendAsEmailChk = new CheckBox();
            enableQueryStringChk = new CheckBox();
            queryStringParameterTxt = new TextBox();
            queryStringFormFieldTxt = new TextBox();
            //Labels
            xsnUriLbl = new Label();
            submitLocationLbl = new Label();
            cssLocationLbl = new Label();
            successActionLbl = new Label();
            customHtmlLbl = new Label();
            redirectUrlLbl = new Label();
            sendAsEmailLbl = new Label();
            enableQueryStringLbl = new Label();
            queryStringParameterLbl = new Label();
            queryStringFormFieldLbl = new Label();
            //Panels
            customHtmlPnl = new Panel();
            redirectUrlPnl = new Panel();
            noActionPnl = new Panel();
            queryStringPnl = new Panel();
            //Buttons
            textBuilderBtn = new Button();

            //Configure the controls
            xsnUriLbl.Text = "XSN Template Location";
            xsnUriLbl.ToolTip = "The path to the form template";
            xsnUriLbl.AssociatedControlID = xsnUriAus.ID;
            xsnUriAus.ID = "xsnUrlAus";
            xsnUriAus.ButtonType = ButtonType.Image;
            xsnUriAus.PickerButtonImage = "/_layouts/1033/images/rtelnk.gif";
            xsnUriAus.DecodeUrlPath = true;
            xsnUriAus.ValidateUrl = true;
            xsnUriAus.AllowExternalUrls = false;
            xsnUriAus.DefaultOpenLocationUrl = SPContext.Current.Site.Url + "/FormServerTemplates/";
            xsnUriAus.OverrideDialogDescription = "Select a form template";
            xsnUriAus.OverrideDialogTitle = "Template Selector";
            xsnUriAus.DefaultToLastUsedLocation = false;
            xsnUriAus.CssClass = "UserInput";
            xsnUriAus.PickerTextBoxWidth = Unit.Pixel(176);
            xsnUriAus.Attributes.Add("ms-T1PnWiden", "true");


            submitLocationLbl.Text = "Submit Location";
            submitLocationLbl.ToolTip = "The path to the list or form library";
            submitLocationLbl.AssociatedControlID = submitLocationAus.ID;
            submitLocationAus.ID = "submitLocationAus";
            submitLocationAus.ButtonType = ButtonType.Image;
            submitLocationAus.PickerButtonImage = "/_layouts/1033/images/rtelnk.gif";
            submitLocationAus.DecodeUrlPath = true;
            submitLocationAus.ValidateUrl = true;
            submitLocationAus.AllowExternalUrls = true;
            submitLocationAus.OverrideDialogDescription = "Select a library";
            submitLocationAus.OverrideDialogTitle = "Library Selector";
            submitLocationAus.DefaultToLastUsedLocation = false;
            submitLocationAus.CssClass = "UserInput";
            submitLocationAus.PickerTextBoxWidth = Unit.Pixel(176);
            submitLocationAus.Attributes.Add("ms-T1PnWiden", "true");

            cssLocationLbl.Text = "CSS Location";
            cssLocationLbl.ToolTip = "The path to the CSS for this form";
            cssLocationLbl.AssociatedControlID = cssLocationAus.ID;
            cssLocationAus.ID = "cssLocationAus";
            cssLocationAus.ButtonType = ButtonType.Image;
            cssLocationAus.PickerButtonImage = "/_layouts/1033/images/rtelnk.gif";
            cssLocationAus.DecodeUrlPath = true;
            cssLocationAus.ValidateUrl = true;
            cssLocationAus.AllowExternalUrls = false;
            cssLocationAus.OverrideDialogDescription = "Select a CSS file";
            cssLocationAus.OverrideDialogTitle = "CSS Selector";
            cssLocationAus.DefaultToLastUsedLocation = false;
            cssLocationAus.CssClass = "UserInput";
            cssLocationAus.PickerTextBoxWidth = Unit.Pixel(176);
            cssLocationAus.Attributes.Add("ms-T1PnWiden", "true");

            successActionLbl.Text = "Success Action";
            successActionLbl.ToolTip = "Action taken after the form is successfully submitted";
            successActionLbl.AssociatedControlID = successActionDdl.ID;
            successActionDdl.ID = "successActionDdl";
            successActionDdl.DataSource = new string[] { "Generic", "Custom", "Redirect", "No Action" };
            successActionDdl.DataBind();
            successActionDdl.AutoPostBack = true;
            successActionDdl.CssClass = "UserInput";

            customHtmlPnl.Visible = false;
            customHtmlLbl.Text = "Custom HTML";
            customHtmlLbl.ToolTip = "HTML that will be displayed after a success if 'Custom' is set for the Success Action";
            customHtmlLbl.AssociatedControlID = customHtmlTxt.ID;
            customHtmlTxt.ID = "customHtmlTxt";
            customHtmlTxt.CssClass = "UserInput";
            customHtmlTxt.Width = Unit.Pixel(176);
            customHtmlTxt.Attributes.Add("ms-T1PnWiden", "true");


            redirectUrlPnl.Visible = false;
            redirectUrlLbl.Text = "Success Page";
            redirectUrlLbl.ToolTip = "The server relative path to the page to redirect to after a success if 'Redirect' is set for the Success Action";
            redirectUrlLbl.AssociatedControlID = redirectUrlAus.ID;
            redirectUrlAus.ID = "redirectUrlTxt";
            redirectUrlAus.ButtonType = ButtonType.Image;
            redirectUrlAus.PickerButtonImage = "/_layouts/1033/images/rtelnk.gif";
            redirectUrlAus.DecodeUrlPath = true;
            redirectUrlAus.ValidateUrl = true;
            redirectUrlAus.AllowExternalUrls = true;
            redirectUrlAus.DefaultOpenLocationUrl = SPContext.Current.Site.Url + "/Pages";
            redirectUrlAus.OverrideDialogDescription = "Select a page to redirect to";
            redirectUrlAus.OverrideDialogTitle = "Page Selector";
            redirectUrlAus.DefaultToLastUsedLocation = false;
            redirectUrlAus.CssClass = "UserInput";
            redirectUrlAus.PickerTextBoxWidth = Unit.Pixel(176);
            redirectUrlAus.Attributes.Add("ms-T1PnWiden", "true");

            sendAsEmailLbl.Text = "Send as Email";
            sendAsEmailLbl.ToolTip = "After a successful submission, send the contents of the form in an email.";
            sendAsEmailLbl.AssociatedControlID = sendAsEmailChk.ID;
            sendAsEmailChk.ID = "sendAsEmailChk";
            sendAsEmailChk.CssClass = "UserInput";

            enableQueryStringLbl.Text = "Enable Query String";
            enableQueryStringLbl.ToolTip = "Enable the population of a form field from a query string parameter.";
            enableQueryStringLbl.AssociatedControlID = enableQueryStringChk.ID;
            enableQueryStringChk.ID = "enableQueryStringChk";
            enableQueryStringChk.CssClass = "UserInput";
            enableQueryStringChk.AutoPostBack = true;

            queryStringPnl.Visible = false;
            queryStringParameterLbl.Text = "Query String Parameter";
            queryStringParameterLbl.ToolTip = "The query string parameter to grab the value from.";
            queryStringParameterLbl.AssociatedControlID = queryStringParameterTxt.ID;
            queryStringParameterTxt.ID = "queryStringParameterTxt";
            queryStringParameterTxt.CssClass = "UserInput";
            queryStringParameterTxt.Width = Unit.Pixel(176);
            queryStringParameterTxt.Attributes.Add("ms-T1PnWiden", "true");

            queryStringFormFieldLbl.Text = "Query String Form Field";
            queryStringFormFieldLbl.ToolTip = "The field to populate with the query string parameter value.";
            queryStringFormFieldLbl.AssociatedControlID = queryStringFormFieldTxt.ID;
            queryStringFormFieldTxt.ID = "queryStringFormFieldTxt";
            queryStringFormFieldTxt.CssClass = "UserInput";
            queryStringFormFieldTxt.Width = Unit.Pixel(176);
            queryStringFormFieldTxt.Attributes.Add("ms-T1PnWiden", "true");

            noActionPnl.Visible = false;

            textBuilderBtn.CssClass = "ms-PropGridBuilderButton";
            textBuilderBtn.Text = "...";
            textBuilderBtn.ToolTip = "Click to use builder";

            //Add the controls
            Controls.Add(new LiteralControl("<div style=\"padding: 1px;\">"));
            Controls.Add(new LiteralControl("<table border=\"0\" cellspacing=\"0\" style=\"width: 100%;\">"));
            Controls.Add(new LiteralControl("<tbody>"));
            #region xsnUri
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            Controls.Add(xsnUriLbl);
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            Controls.Add(new LiteralControl("<nobr>"));
            Controls.Add(xsnUriAus);
            Controls.Add(new LiteralControl("</nobr>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region submitLocation
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            Controls.Add(submitLocationLbl);
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            Controls.Add(new LiteralControl("<nobr>"));
            Controls.Add(submitLocationAus);
            Controls.Add(new LiteralControl("</nobr>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<p>"));
            Controls.Add(new LiteralControl(@userAccount));
            Controls.Add(new LiteralControl(" must have Contribute rights to the above location."));
            Controls.Add(new LiteralControl("</p>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region cssLocation
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            Controls.Add(cssLocationLbl);
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            Controls.Add(new LiteralControl("<nobr>"));
            Controls.Add(cssLocationAus);
            Controls.Add(new LiteralControl("</nobr>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region successAction
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            Controls.Add(successActionLbl);
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            Controls.Add(new LiteralControl("<nobr>"));
            Controls.Add(successActionDdl);
            Controls.Add(new LiteralControl("</nobr>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region customHtml
            Controls.Add(customHtmlPnl);
            customHtmlPnl.Controls.Add(new LiteralControl("<tr>"));
            customHtmlPnl.Controls.Add(new LiteralControl("<td>"));
            customHtmlPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            customHtmlPnl.Controls.Add(customHtmlLbl);
            customHtmlPnl.Controls.Add(new LiteralControl("</div>"));
            customHtmlPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            customHtmlPnl.Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            customHtmlPnl.Controls.Add(new LiteralControl("<nobr>"));
            customHtmlPnl.Controls.Add(customHtmlTxt);
            customHtmlPnl.Controls.Add(new LiteralControl("&nbsp"));
            customHtmlPnl.Controls.Add(textBuilderBtn);
            customHtmlPnl.Controls.Add(new LiteralControl("</nobr>"));
            customHtmlPnl.Controls.Add(new LiteralControl("</div>"));
            customHtmlPnl.Controls.Add(new LiteralControl("</div>"));
            customHtmlPnl.Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            customHtmlPnl.Controls.Add(new LiteralControl("</td>"));
            customHtmlPnl.Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region redirectUrl
            Controls.Add(redirectUrlPnl);
            redirectUrlPnl.Controls.Add(new LiteralControl("<tr>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("<td>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            redirectUrlPnl.Controls.Add(redirectUrlLbl);
            redirectUrlPnl.Controls.Add(new LiteralControl("</div>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            redirectUrlPnl.Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            redirectUrlPnl.Controls.Add(new LiteralControl("<nobr>"));
            redirectUrlPnl.Controls.Add(redirectUrlAus);
            redirectUrlPnl.Controls.Add(new LiteralControl("</nobr>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("</div>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("</div>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("</td>"));
            redirectUrlPnl.Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region sendAsEmail
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            Controls.Add(sendAsEmailLbl);
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            Controls.Add(new LiteralControl("<nobr>"));
            Controls.Add(sendAsEmailChk);
            Controls.Add(new LiteralControl("</nobr>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region enableQueryString
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            Controls.Add(enableQueryStringLbl);
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            Controls.Add(new LiteralControl("<nobr>"));
            Controls.Add(enableQueryStringChk);
            Controls.Add(new LiteralControl("</nobr>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("</div>"));
            Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region queryStringParameter
            Controls.Add(queryStringPnl);
            queryStringPnl.Controls.Add(new LiteralControl("<tr>"));
            queryStringPnl.Controls.Add(new LiteralControl("<td>"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            queryStringPnl.Controls.Add(queryStringParameterLbl);
            queryStringPnl.Controls.Add(new LiteralControl("</div>"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            queryStringPnl.Controls.Add(new LiteralControl("<nobr>"));
            queryStringPnl.Controls.Add(queryStringParameterTxt);
            queryStringPnl.Controls.Add(new LiteralControl("</nobr>"));
            queryStringPnl.Controls.Add(new LiteralControl("</div>"));
            queryStringPnl.Controls.Add(new LiteralControl("</div>"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            queryStringPnl.Controls.Add(new LiteralControl("</td>"));
            queryStringPnl.Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region queryStringFormField
            queryStringPnl.Controls.Add(new LiteralControl("<tr>"));
            queryStringPnl.Controls.Add(new LiteralControl("<td>"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            queryStringPnl.Controls.Add(queryStringFormFieldLbl);
            queryStringPnl.Controls.Add(new LiteralControl("</div>"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            queryStringPnl.Controls.Add(new LiteralControl("<nobr>"));
            queryStringPnl.Controls.Add(queryStringFormFieldTxt);
            queryStringPnl.Controls.Add(new LiteralControl("</nobr>"));
            queryStringPnl.Controls.Add(new LiteralControl("</div>"));
            queryStringPnl.Controls.Add(new LiteralControl("</div>"));
            queryStringPnl.Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            queryStringPnl.Controls.Add(new LiteralControl("</td>"));
            queryStringPnl.Controls.Add(new LiteralControl("</tr>"));
            #endregion
            #region noAction
            Controls.Add(noActionPnl);
            noActionPnl.Controls.Add(new LiteralControl("<tr>"));
            noActionPnl.Controls.Add(new LiteralControl("<td>"));
            noActionPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionHead\">"));
            noActionPnl.Controls.Add(new LiteralControl("</div>"));
            noActionPnl.Controls.Add(new LiteralControl("<div class=\"UserSectionBody\">"));
            noActionPnl.Controls.Add(new LiteralControl("<div class=\"UserControlGroup\">"));
            noActionPnl.Controls.Add(new LiteralControl("<nobr>"));
            noActionPnl.Controls.Add(new LiteralControl("</nobr>"));
            noActionPnl.Controls.Add(new LiteralControl("</div>"));
            noActionPnl.Controls.Add(new LiteralControl("<p>"));
            noActionPnl.Controls.Add(new LiteralControl("No action will be taken by the webpart upon a successful submission."));
            noActionPnl.Controls.Add(new LiteralControl("</p>"));
            noActionPnl.Controls.Add(new LiteralControl("</div"));
            noActionPnl.Controls.Add(new LiteralControl("<div class=\"UserDottedLine\" width=\"100%\"/>"));
            noActionPnl.Controls.Add(new LiteralControl("</td>"));
            noActionPnl.Controls.Add(new LiteralControl("</tr>"));
            #endregion
            Controls.Add(new LiteralControl("</tbody>"));
            Controls.Add(new LiteralControl("</table>"));
            Controls.Add(new LiteralControl("</div>"));

            //More configuration
            customHtmlTxt.Attributes.Add("onfocusin",
                string.Format("MSOPGrid_BuilderVisible({0})", textBuilderBtn.ClientID.ToString()));
            //customHtmlTxt.Attributes.Add("onfocusout",
            //    string.Format("{0}.style.display='none';", textBuilderBtn.ClientID.ToString()));
            customHtmlTxt.Attributes.Add("ondeactivate",
                string.Format("MSOT1Pn_prevBuilder={0};", textBuilderBtn.ClientID.ToString()));

            textBuilderBtn.Attributes.Add("onclick",
                string.Format(@"javascript:MSOPGrid_doBuilder('\u002f_layouts\u002fzoombldr.aspx?culture=en-US' ,{0},
                'dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes'); return false;",
                customHtmlTxt.ClientID.ToString()));

        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //Set Visibility
            if (successActionDdl.SelectedValue == "Generic")
            {
                customHtmlPnl.Visible = false;
                redirectUrlPnl.Visible = false;
                noActionPnl.Visible = false;
            }

            if (successActionDdl.SelectedValue == "Custom")
            {
                customHtmlPnl.Visible = true;
                redirectUrlPnl.Visible = false;
                noActionPnl.Visible = false;
            }

            if (successActionDdl.SelectedValue == "Redirect")
            {
                customHtmlPnl.Visible = false;
                redirectUrlPnl.Visible = true;
                noActionPnl.Visible = false;
            }

            if (successActionDdl.SelectedValue == "No Action")
            {
                customHtmlPnl.Visible = false;
                redirectUrlPnl.Visible = false;
                noActionPnl.Visible = true;
            }

            if (enableQueryStringChk.Checked)
            {
                queryStringPnl.Visible = true;
            }
            else
            {
                queryStringPnl.Visible = false;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
    }
}
